//
//  MIXMianTabBar.m
//  WEIBO
//
//  Created by Mix on 16/5/12.
//  Copyright © 2016年 Mix. All rights reserved.
//

#import "MIXMianTabBar.h"

@interface MIXMianTabBar ()

@property(nonatomic,strong)UIButton * composeButton;


@end

@implementation MIXMianTabBar


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]) {
        //添加+号按钮
        UIButton * composeButton = [[UIButton alloc] init];
        [composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        self.composeButton = composeButton;
        
        //添加绑定事件
        [composeButton addTarget:self action:@selector(didClickRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
        
        //添加到tabBar
        [self addSubview:composeButton];
    }
    
    return self;
    
}

//布局子控件--同时重新布局tabBar其他四个字控制器的按钮
-(void)layoutSubviews{
    [super layoutSubviews];
    
    //计算
    CGFloat width = self.frame.size.width / 5;
    
    //设置索引
    NSInteger index = 0;
    for (UIView * view  in self.subviews) {
        
        NSLog(@"view:%@",view);
        
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            view.frame = CGRectMake(index * width, 0, width, self.frame.size.height);
            index ++;
            if (index == 2) {
                index ++;
            }
        }
        
        self.composeButton.frame = CGRectMake(2 * width, 0, width, self.frame.size.height);
        
    }
}


#pragma mark -- +号点击事件
-(void)didClickRegisterBtn{
    //modal出一个控制器，在view中是无法modal出一个控制器的，所以通过block传递事件，让控制器来完成这个modal
    //block回调
    if (self.callBlock) {
        self.callBlock();
    }
}





@end

